﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Models;
using BLL;

namespace Player.ShangCheng
{
    public partial class SCwegm1 : System.Web.UI.Page
    {
        //窗体加载事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                #region //展示对应游戏信息
                if (Request.QueryString["id"] != null)
                {
                    //查询指定游戏id的信息
                    int id = Convert.ToInt32(Request.QueryString["id"]);
                    List<Game> ls = BLL.GameManage.SeleIdGame(id);
                    //绑定显示视频
                    this.rpttv.DataSource = ls;
                    this.rpttv.DataBind();
                    //游戏名称
                    this.lblname.Text = ls[0].Gm_Name;
                    //简介
                    this.lbldesc.Text = ls[0].Gm_Desc;
                    //价格
                    string price = ls[0].Gm_Price.ToString();
                    if (price == "0")
                    {
                        this.lblprice.Text = "免费";
                        this.btn_GouMai.Text = "免费下载";
                    }
                    else
                    {
                        this.btn_GouMai.Text = "点击购买";
                        this.lblprice.Text = "￥" + price;
                    }
                    //判断是否以下载
                    int usid=0;
                    if (Session["usid"]!=null)
                    {
                        usid = Convert.ToInt32(Session["usid"]);
                    }
                    List<Orders> pd = BLL.OrdersManage.SeleUsGmOrders(usid, id);
                    if (pd.Count != 0)
                    {
                        this.btn_GouMai.Text = "已下载";
                        this.btn_GouMai.Enabled = false;
                    }
                    //图片
                    this.Image1.ImageUrl = "~/Player图片/游戏图片/" + ls[0].Gm_Image;
                    //名称2
                    this.lblname2.Text = ls[0].Gm_Name;
                    //游戏类型
                    int typeid = ls[0].Gm_TypeId - 48;
                    List<GmType> ls2 = BLL.GmTypeManage.SeleIdGmType(typeid);
                    this.lblgmtype.Text = ls2[0].Gt_Name.ToString();
                    //价格2
                    this.lblprice2.Text = ls[0].Gm_Price.ToString();
                    //点赞数量
                    this.lblupcount.Text = ls[0].Gm_Upcount.ToString();
                }
                #endregion
            }
        }

        //点赞按钮
        protected void imgbtn_dianzan_Click(object sender, ImageClickEventArgs e)
        {
            //获取点赞数并加1
            Game gm = new Game();
            gm.Gm_Id = Convert.ToInt32(Request.QueryString["id"]);
            gm.Gm_Upcount = Convert.ToInt32(this.lblupcount.Text) + 1;
            //修改
            bool bl = BLL.GameManage.UpdaCountGame(gm);
            if (bl)
            {
                //更新点赞数量
                int id = Convert.ToInt32(Request.QueryString["id"]);
                List<Game> ls = BLL.GameManage.SeleIdGame(id);
                this.lblupcount.Text = ls[0].Gm_Upcount.ToString();
                this.imgbtn_dianzan.ImageUrl = "~/Player图片/图标/游戏页面1/dianzan2.png";
                this.imgbtn_dianzan.Enabled = false;
            }
        }

        //购买下载
        protected void btn_GouMai_Click(object sender, EventArgs e)
        {
            //用户id
            int id = Convert.ToInt32(Session["usid"]);
            //游戏价格
            double price = Convert.ToDouble(this.lblprice2.Text.Trim());
            //查询用户信息
            List<Users> ls = BLL.UsersManage.SeleIdUsers(id);

            if (price==0)
            {
                this.btn_GouMai.Text = "下载中";
            }
            else
            {
                //显示支付匡
                this.pl_goumai.Visible = true;
                //隐藏
                this.btn_GM2.Enabled = true;
                this.ZFTS.Visible = false;
                //获取游戏信息并给予页面相应内容
                int gmid = Convert.ToInt32(Request.QueryString["id"]);
                List<Game> ls2 = BLL.GameManage.SeleIdGame(gmid);
                this.img_zfxx1.ImageUrl = "../Player图片/游戏图片/" + ls2[0].Gm_Image;
                this.lbl_zfxx2.Text = ls2[0].Gm_Name;
                this.lbl_zfxx3.Text = ls2[0].Gm_Price.ToString();
                int goid = ls[0].Us_Goid;
                this.lbl_zhifugoid.Text = goid.ToString();
                int goidjg=(goid / 10);
                if (goid>=ls2[0].Gm_Price)
	            {
                    this.lbl_zhifudkprice.Text = "全部费用";
	            }
                else
                {
                    this.lbl_zhifudkprice.Text = "￥" + goidjg.ToString();
                }
                this.lbl_ZFlastprice.Text = ls2[0].Gm_Price.ToString();
            }
        }

        //影藏购买选择
        protected void imgbtn_guanbi_Click(object sender, ImageClickEventArgs e)
        {
            this.pl_goumai.Visible = false;
        }

        //支付
        protected void btn_GM2_Click(object sender, EventArgs e)
        {
            //用户id
            int id = Convert.ToInt32(Session["usid"]);
            //查询用户信息
            List<Users> ls = BLL.UsersManage.SeleIdUsers(id);
            //获取账户余额
            double price = ls[0].Us_Balance;
            //需支付
            double price2 = Convert.ToDouble(this.lbl_ZFlastprice.Text);
            //获取账户金币余额
            int goid=ls[0].Us_Goid;
            //获取支付金币
            int jb = Convert.ToInt32(this.lbl_zhifugoid.Text)/10;
            int goid2;
            if (jb>=price2)
            {
                goid2 = Convert.ToInt32(price2*10);
            }
            else
            {
                goid2 = Convert.ToInt32(jb * 10);
            }
            //修改余额
            Users us = new Users();
            us.Us_Id = id;
            us.Us_Balance = price - price2;
            us.Us_Goid = goid - goid2;
            bool bl = BLL.UsersManage.UpdaPriceUsers(us);
            if (bl)
            {
                this.ZFTS.Visible = true;
                this.ZFTS.Text = "支付成功";
                //改变下载按钮状态
                this.btn_GouMai.Text = "已下载";
                this.btn_GouMai.Enabled = false;
                //添加订单信息
                Orders or = new Orders();
                or.Or_UsId = Convert.ToInt32(Session["usid"]);
                or.Or_GmId=Convert.ToInt32(Request.QueryString["id"]);
                or.Or_Goid = goid2;
                or.Or_Price = price2;
                or.Or_Date = Convert.ToDateTime(DateTime.Now.ToString());
                bool bl2 = BLL.OrdersManage.AddAllOrders(or);
                this.btn_GM2.Enabled = false;
            }
            else
            {
                this.ZFTS.Text = "支付失败";
            }

        }

    }

}